<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getBalanceTotal } from '@/api/dashboard'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      timeout: null,
      chartData: {}
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  destroyed() {
    if (this.timeout) {
      clearInterval(this.timeout)
      this.timeout = null
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.loadData()
      this.timeout = setInterval(() => {
        this.loadData()
      }, 1000 * 60)
    },
    loadData() {
      getBalanceTotal().then(({ success, message, data }) => {
        if (success) {
          const { charges, pays } = data || {}
          for (let i = 0; i < charges.length; ++i) {
            charges[i] = this.formatAmount(charges[i])
            pays[i] = this.formatAmount(pays[i])
          }
          this.chartData = { charges, pays }
        }
      })
    },
    formatAmount(value) {
      let amount = parseInt(value)
      if (isNaN(amount)) {
        return 0
      }
      amount = amount / 100
      let s = amount.toString()
      let idx = s.indexOf('.')
      if (idx < 0) {
        idx = s.length
        s += '.'
      }
      while (s.length < (idx + 3)) {
        s += 0
      }
      return parseFloat(s)
    },
    setOptions({ charges, pays } = {}) {
      this.chart.setOption({
        title: {
          text: '费用支付统计(单位：元)',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          data: ['充值', '支付'],
          left: 'right'
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['今天', '本周', '本月', '本季', '本年'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '充值',
          type: 'bar',
          data: charges,
          animationDuration
        }, {
          name: '支付',
          type: 'bar',
          data: pays,
          animationDuration
        }]
      })
    }
  }
}
</script>
